/**
 * Created by cuppi on 2017/6/29.
 */
var express = require('express');
var router = express.Router();
var FaceManager = require('../ai/FaceManager');
var path = require('path');
var fs = require('fs');
var NetworkManager = require('../network/NetworkManager');

/* GET users listing. */
router.get('/', function (req, res, next) {
    let imageUrl = req.query.baseImgUrl;
    if (req.body.baseImgUrl){
        imageUrl = req.body.baseImgUrl;
    }
    if (!imageUrl) {
        res.json({data: {}, errorCode: 1, message: 'baseImgUrl is not found'});
        return;
    }
    FaceManager.defaultManager().identifyUserFromUrl(imageUrl).then(data => {
        res.json({data: data, errorCode: 0, message: ''});
    }, error => {
        res.send(error);
    });

});

router.post('/', function (req, res, next) {
    let faceImageUrl = req.query.baseImgUrl;
    if (req.body.baseImgUrl){
        faceImageUrl = req.body.baseImgUrl;
    }

    let base64Image = req.query.base64Image;
    if (req.body.base64Image){
        base64Image = req.body.base64Image;
    }

    if (!faceImageUrl && !base64Image) {
        res.json({data: {}, errorCode: 1, message: 'baseImgUrl & base64Image is not found'});
        return;
    }

    if (base64Image){
        FaceManager.defaultManager().identifyUserFromBase64Image(base64Image).then(data => {
            console.log(data);
            res.json({data: data, errorCode: 0, message: ''});
        }, error => {
            res.send(error);
        });
    } else if (faceImageUrl){
        FaceManager.defaultManager().identifyUserFromUrl(faceImageUrl).then(data => {
            console.log(data);
            res.json({data: data, errorCode: 0, message: ''});
        }, error => {
            res.send(error);
        });
    }

});

module.exports = router;